﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using ComicIsland.Common;
using ImageTools;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Foundation.Metadata;

namespace ComicIsland.App
{
    public partial class ProgressStatusBar : UserControl
    {
        #region ToolTipForeground
        /// <summary>
        /// 注册依赖属性
        /// </summary>
        public static DependencyProperty ToolTipForegroundProperty = DependencyProperty.Register("ToolTipForeground",
                                           typeof(SolidColorBrush),
                                           typeof(ProgressStatusBar),
                                           new PropertyMetadata(null, OnToolTipForegroundPropertyChanged));
        //new PropertyMetadata(Application.Current.Resources["DefaultBackgroundBrush"] as SolidColorBrush, OnToolTipForegroundPropertyChanged));

        /// <summary>
        /// 依赖属性对应的控件属性
        /// </summary>
        public SolidColorBrush ToolTipForeground
        {
            get { return (SolidColorBrush)GetValue(ToolTipForegroundProperty); }
            set { SetValue(ToolTipForegroundProperty, value); }
        }

        /// <summary>
        /// 依赖属性回掉函数
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnToolTipForegroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //这里MySilverButton是控件类名
            var psbar = d as ProgressStatusBar;
            if (psbar != null)
                psbar.OnToolTipForegroundPropertyChanged(e);
        }

        //属性变化回掉（通过静态函数调用，也可以在静态函数里写）
        private void OnToolTipForegroundPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                //this.txtToolTip.Foreground = new SolidColorBrush(Helper.HexToColor(e.NewValue as string));
                this.txtToolTip.Foreground = e.NewValue as SolidColorBrush;
                this.progressBar.Foreground = e.NewValue as SolidColorBrush;
            }
        }
        #endregion ToolTipForeground

        #region UserControlBackground
        /// <summary>
        /// 注册依赖属性
        /// </summary>
        public static DependencyProperty UserControlBackgroundProperty = DependencyProperty.Register("UserControlBackground",
                                           typeof(SolidColorBrush),
                                           typeof(ProgressStatusBar),
                                           new PropertyMetadata(null, OnUserControlBackgroundPropertyChanged));
        //new PropertyMetadata(Application.Current.Resources["DefaultBackgroundBrush"] as SolidColorBrush, OnToolTipForegroundPropertyChanged));

        /// <summary>
        /// 依赖属性对应的控件属性
        /// </summary>
        public SolidColorBrush UserControlBackground
        {
            get { return (SolidColorBrush)GetValue(UserControlBackgroundProperty); }
            set { SetValue(UserControlBackgroundProperty, value); }
        }

        /// <summary>
        /// 依赖属性回掉函数
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnUserControlBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //这里MySilverButton是控件类名
            var psbar = d as ProgressStatusBar;
            if (psbar != null)
                psbar.OnUserControlBackgroundPropertyChanged(e);
        }

        //属性变化回掉（通过静态函数调用，也可以在静态函数里写）
        private void OnUserControlBackgroundPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                this.statusBar.Background = e.NewValue as SolidColorBrush;
            }
        }
        #endregion UserControlBackground

        public ProgressStatusBar()
        {
            InitializeComponent();
            //
            StatusBarLoadingGIF();
        }

        private void StatusBarLoadingGIF()
        {
            ImageTools.IO.Decoders.AddDecoder<ImageTools.IO.Gif.GifDecoder>();
            ExtendedImage image = new ExtendedImage();
            image.UriSource = new Uri(KEYS.RESOURCE_ASSEMBLYNAME + "/Images/loading.gif", UriKind.Relative);
            this.imageGif.Source = image;
        }

        public void OpenStatusBar(string infomation)
        {
            this.statusBar.Visibility = System.Windows.Visibility.Visible;
            this.txtToolTip.Text = infomation;
        }

        public void CloseStatusBar()
        {
            this.statusBar.Visibility = System.Windows.Visibility.Collapsed;
            this.txtToolTip.Text = string.Empty;
        }
    }
}
